package com.xidian.zhiancloud.controller.workTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.workTicket.ConfinedSpaceTicketVODTO;
import com.xidian.zhiancloud.dto.workTicket.HighTicketVODTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.workTicket.ConfinedSpaceTicket;
import com.xidian.zhiancloud.model.workTicket.HighTicket;
import com.xidian.zhiancloud.qo.workTicket.ConfinedSpaceTicketQO;
import com.xidian.zhiancloud.qo.workTicket.HighTicketQO;
import com.xidian.zhiancloud.service.workTicket.*;
import com.xidian.zhiancloud.util.zyf.RedisUtil;
import com.xidian.zhiancloud.vo.workTicket.ConfinedSpaceTicketVO;
import com.xidian.zhiancloud.vo.workTicket.HighTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xidian.zhiancloud.service.workTicket.ConfinedSpaceService;

import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("ConfinedSpace")
public class ConfinedSpaceController extends BaseController {
    @Autowired
    private ConfinedSpaceService confinedSpaceService;

    @Autowired
    private ConfinedFlowService confinedFlowService;

    @Autowired
    private RedisUtil redisUtil;
    /**
     * 申请作业票
     *
     * @param confinedSpaceTicket
     * @return
     */
    @PostMapping("/addConfinedSpaceTicket")
    public ResultDTO<Boolean> addConfinedSpaceTicket(ConfinedSpaceTicket confinedSpaceTicket) {
        confinedSpaceTicket.setCompanyId(getSessionUserCompanyId());
        confinedSpaceTicket.setApplicantId(getSessionUserid());
        confinedSpaceTicket.setCreateTime(new Date(System.currentTimeMillis()));
//        highTicket.setApplicantUnitId();
        // 设置危险作业票id
        confinedSpaceTicket.setWorkNumber(redisUtil.generateWorkId(getSessionUserCompanyId(), confinedSpaceTicket.getApplicantUnitId()));
        confinedSpaceTicket.setIsCommit1(1);
        confinedSpaceTicket.setCurrentStep(2);
        Boolean res = confinedSpaceService.save(confinedSpaceTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除待提交作业票
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteConfinedSpaceTicket")
    public ResultDTO<Boolean> deleteConfinedSpaceTicket(@RequestParam("ids") Long[] ids) {
        if (ids == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        Boolean res = confinedSpaceService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查询作业票
     *
     * @param id
     * @return
     */
    @GetMapping("/getConfinedSpaceTicket")
    public ResultDTO<ConfinedSpaceTicketVODTO> getConfinedSpaceTicket(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        Long companyId = getSessionUserCompanyId();
        // 返回DTO对象，为highTicketVO及操作权限标识Map
        ConfinedSpaceTicketVODTO confinedSpaceTicketVODTO = confinedSpaceService.getConfinedSpaceTicketVODTOById(companyId, id);
        return ResultDTO.successOf(confinedSpaceTicketVODTO, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查找
     *
     * @param confinedSpaceTicketQO
     * @return
     */
    @GetMapping("getConfinedSpaceTicketList")
    public ResultDTO<IPage<ConfinedSpaceTicketVO>> getConfinedSpaceTicketList(ConfinedSpaceTicketQO confinedSpaceTicketQO) {
        if (confinedSpaceTicketQO.getSize() == null || confinedSpaceTicketQO.getCurrent() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        confinedSpaceTicketQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(confinedSpaceService.getListByQO(confinedSpaceTicketQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 作业票审批
     *
     * @param confinedSpaceTicket
     * @return
     */
    @PostMapping("/approveConfinedSpaceTicket")
    public ResultDTO<Boolean> approveConfinedSpaceTicket(ConfinedSpaceTicket confinedSpaceTicket) {
        Long ticketId = confinedSpaceTicket.getId();
        // 前端的fireTicket在根据id更新后端的记录时，不对以下字段做修改
        confinedSpaceTicket.setUpdateTime(new Date(System.currentTimeMillis()));
        confinedSpaceTicket.setCompanyId(null);
        confinedSpaceTicket.setCreateTime(null);
        confinedSpaceTicket.setIsCommit1(null);
        confinedSpaceTicket.setIsCommit2(null);
        confinedSpaceTicket.setIsCommit3(null);
        confinedSpaceTicket.setIsCommit4(null);
        confinedSpaceTicket.setIsCommit5(null);
        confinedSpaceTicket.setIsCommit6(null);
        confinedSpaceTicket.setIsCommit7(null);
        confinedSpaceTicket.setIsCommit8(null);
        confinedSpaceTicket.setIsCommit9(null);
        confinedSpaceTicket.setIsCommit10(null);
        confinedSpaceTicket.setIsCommit11(null);
        confinedSpaceTicket.setIsCommit12(null);

        // 根据id查询数据库已有数据
        ConfinedSpaceTicket confinedSpaceTicketData = confinedSpaceService.getById(ticketId);
        Integer currentStep = confinedSpaceTicketData.getCurrentStep();

        Date nowTime = new Date(System.currentTimeMillis());

        //  判断是哪一个小步骤完成了提交
        Integer subStep = confinedFlowService.getFinishSubId(currentStep, confinedSpaceTicket);
        // 将该小步骤步中的isCommit**设置为1，并填充时间
        confinedSpaceService.setSubStepFinish(subStep, confinedSpaceTicket);

        // 保存
        Boolean res1 = confinedSpaceService.updateById(confinedSpaceTicket);
        // 获取保存后的ticket
        ConfinedSpaceTicket newconfinedSpaceTicket = confinedSpaceService.getById(ticketId);
        // 判断本阶段作业流是否已经结束
        Integer updateStep = confinedFlowService.updateCurrentStep(currentStep, newconfinedSpaceTicket);
        // 更新当前大步骤的状态
        newconfinedSpaceTicket.setCurrentStep(updateStep);
        Boolean res = confinedSpaceService.updateById(newconfinedSpaceTicket);
        return ResultDTO.successOf(res || res1, GeneralResponseEnums.SUCCESS);
    }
}
